from nCoVweb.Control import blue
from flask import render_template
import json
from nCoVweb.model.SQLmodel import SQLmodel as city,SQLtotal as total


@blue.route('/')
def hello_world():
    return 'Hello World!'

@blue.route('/2019-ncov/test')
def ncov():
    return render_template('/tpl/index.html')

#map全国疫情分布图
@blue.route('/data_city')
def bigdata():
    data=city.query.with_entities(city.city,city.diagnosed).all()
    tmp=''
    a=[]

    for i in data:
        b = {}
        b["name"]=i[0]
        b["value"]=i[1]
        # tmp=tmp+str({'name':i[0],'value':i[1]})
        a.append(b)
    json_data=json.dumps(a,ensure_ascii=False)
    return json_data

#bar排名情况
@blue.route('/data_bar')
def bars():
    data=city.query.with_entities(city.city,city.diagnosed).order_by(city.diagnosed.desc()).limit(10).all()
    tmp=[]
    for i in data:
        tmps={}
        tmps["name"]=i[0]
        tmps["value"]=i[1]
        tmp.append(tmps)
    json_data=json.dumps(tmp,ensure_ascii=False)
    return json_data

@blue.route("/data_table")
def tables():
    data=total.query.with_entities(total.dates,total.diagnosed,total.suspected,total.cured,total.died).order_by(total.diagnosed.desc()).limit(3).all()
    tmp=[]
    for i in data:
        tmps={}
        tmps["date"]=i[0]
        tmps["diagnosed"]=i[1]
        tmps["suspected"]=i[2]
        tmps["cured"]=i[3]
        tmps["died"]=i[4]
        tmp.append(tmps)
    json_data=json.dumps(tmp,ensure_ascii=False)
    return json_data

@blue.route("/data_line_bar")
def line_bar():
    data=total.query.with_entities(total.dates,total.diagnosed,total.suspected,total.cured,total.died).order_by(total.diagnosed.desc()).limit(23).all()
    tmp = []
    for i in data:
        tmps = {}
        tmps["date"] = i[0]
        tmps["diagnosed"] = i[1]
        tmps["suspected"] = i[2]
        tmps["cured"] = i[3]
        tmps["died"] = i[4]
        tmp.append(tmps)
    json_data = json.dumps(tmp, ensure_ascii=False)
    return json_data

#整体数据
@blue.route("/data_text")
def data_te():
    data = total.query.with_entities(total.dates, total.diagnosed, total.suspected, total.cured, total.died).order_by(total.diagnosed.desc()).limit(1).all()
    tmp = []
    for i in data:
        tmps = {}
        tmps["date"] = i[0]
        tmps["diagnosed"] = i[1]
        tmps["suspected"] = i[2]
        tmps["cured"] = i[3]
        tmps["died"] = i[4]
        tmp.append(tmps)
    json_data = json.dumps(tmp,ensure_ascii=False)
    return json_data
@blue.route('/data_test')
def da_test():
    return render_template('/test/test.html')

@blue.route('/data_views')
def da_tesd():
    return render_template('/view.html')